package com.xiaoyu.awakening.common.miniprogram;

import com.xiaoyu.awakening.common.miniprogram.dto.req.WxMinProgramBaseReq;
import com.xiaoyu.awakening.common.miniprogram.dto.req.WxMinProgramSendMsgReq;
import com.xiaoyu.awakening.common.miniprogram.dto.req.WxMinProgramStableTokenReq;
import com.xiaoyu.awakening.common.miniprogram.dto.res.WxMinProgramBaseRes;
import com.xiaoyu.awakening.common.miniprogram.dto.res.WxMinProgramJsCodeToSessionRes;
import com.xiaoyu.awakening.common.miniprogram.dto.res.WxMinProgramPhoneNumberRes;
import com.xiaoyu.awakening.common.miniprogram.dto.res.WxMinProgramTokenRes;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 微信小程序服务端API:
 * <a href="https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/">...</a>
 *
 * @author wangxiaoyu
 * @date 2024/6/14 11:16
 * @since v1.0
 */
@FeignClient(url = "https://api.weixin.qq.com", name = "wxMinProgramApi")
public interface WxMinProgramApi {
    /**
     * 获取接口调用凭据
     *
     * @param grant_type 填写 client_credential
     * @param appid      小程序唯一凭证，即 AppID
     * @param secret     小程序唯一凭证密钥
     * @author wangxiaoyu
     * @date 2024/6/14 11:14
     * @since v1.0
     */
    @GetMapping("/cgi-bin/token")
    WxMinProgramTokenRes getAccessToken(@RequestParam(value = "grant_type", defaultValue = "client_credential") String grant_type,
                                        @RequestParam("appid") String appid,
                                        @RequestParam("secret") String secret);

    /**
     * 获取稳定版接口调用凭据
     *
     * @author wangxiaoyu
     * @date 2024/6/14 11:40
     * @since v1.0
     */
    @PostMapping("/cgi-bin/stable_token")
    WxMinProgramTokenRes getStableAccessToken(@RequestBody WxMinProgramStableTokenReq wxMinProgramStableTokenReq);

    /**
     * 小程序登录
     *
     * @param appid      小程序唯一凭证，即 AppID
     * @param secret     小程序唯一凭证密钥
     * @param js_code    登录时获取的 code，可通过wx.login获取
     * @param grant_type 授权类型，此处只需填写 authorization_code
     * @author wangxiaoyu
     * @date 2024/6/14 11:51
     * @since v1.0
     */
    @GetMapping("/sns/jscode2session")
    WxMinProgramJsCodeToSessionRes code2Session(@RequestParam("appid") String appid,
                                                @RequestParam("secret") String secret,
                                                @RequestParam(value = "js_code") String js_code,
                                                @RequestParam(value = "grant_type", defaultValue = "authorization_code") String grant_type
    );

    /**
     * 检验登录态
     *
     * @param access_token 接口调用凭证，该参数为 URL 参数，非 Body 参数。使用access_token或者authorizer_access_token
     * @param openid       用户唯一标识符
     * @param signature    用户登录态签名，用session_key对空字符串签名得到的结果。即 signature = hmac_sha256(session_key, "")
     * @param sig_method   用户登录态签名的哈希方法，目前只支持 mac_sha256
     * @author wangxiaoyu
     * @date 2024/6/14 11:52
     * @since v1.0
     */
    @GetMapping("/wxa/checksession")
    WxMinProgramBaseRes checkSessionKey(@RequestParam("access_token") String access_token,
                                        @RequestParam("openid") String openid,
                                        @RequestParam(value = "signature") String signature,
                                        @RequestParam(value = "sig_method", defaultValue = "mac_sha256") String sig_method
    );

    /**
     * 获取手机号
     *
     * @param access_token 接口调用凭证，该参数为 URL 参数，非 Body 参数。使用access_token或者authorizer_access_token
     * @author wangxiaoyu
     * @date 2024/6/14 12:03
     * @since v1.0
     */
    @PostMapping("/wxa/business/getuserphonenumber")
    WxMinProgramPhoneNumberRes getPhoneNumber(@RequestParam("access_token") String access_token,
                                              @RequestBody WxMinProgramBaseReq wxMinProgramBaseReq);

    /**
     * 发送订阅消息
     *
     * @param access_token           接口调用凭证，该参数为 URL 参数，非 Body 参数。使用access_token或者authorizer_access_token
     * @param wxMinProgramSendMsgReq wxMinProgramSendMsgReq
     * @author wangxiaoyu
     * @date 2024/6/14 13:48
     * @since v1.0
     */
    @PostMapping("/cgi-bin/message/subscribe/send")
    WxMinProgramBaseRes sendMessage(@RequestParam("access_token") String access_token,
                                    @RequestBody WxMinProgramSendMsgReq wxMinProgramSendMsgReq);


}
